package com.yaohe.consumer.core;

import android.content.Context;
import android.content.SharedPreferences;

public class XmlManager {

	private static Context context;
	
	public XmlManager(Context contxt){
		this.context = contxt;
	}
	
	public static boolean isExistValue(String fileName, String keyword, Context context){
		SharedPreferences sp = context.getSharedPreferences(fileName, 0);
		if(sp.contains(keyword)){
			return true;
		}else{
			return false;
		}
	}
	
	public static void setValue(String fileName, String keyword, Object object, Context context){
		SharedPreferences sp = context.getSharedPreferences(fileName, 0);
		SharedPreferences.Editor editor = sp.edit();
		
		if(object instanceof Boolean){
			editor.putBoolean(keyword, (Boolean) object);
		}else if(object instanceof String){
			editor.putString(keyword, (String) object);
		}else if(object instanceof Float){
			editor.putFloat(keyword, (Float)object);
		}else if(object instanceof Long){
			editor.putLong(keyword, (Long)object);
		}else if(object instanceof Integer){
			editor.putInt(keyword, (Integer)object);
		}
		
		editor.commit();
	}
	
	public static Object getValue(String fileName, String keyword, String type, Context context){
		SharedPreferences sp = context.getSharedPreferences(fileName, 0);

		Object value = null;
		if(type.equals("boolean")){
			value = sp.getBoolean(keyword, XmlInfo.DEFAULT_BOOL); 
		}else if(type.equals("string")){
			value = sp.getString(keyword, XmlInfo.DEFAULT_STRING);
		}else if(type.equals("int")){
			value = sp.getInt(keyword, XmlInfo.DEFAULT_INT);
		}else if(type.equals("float")){
			value = sp.getFloat(keyword, XmlInfo.DEFAULT_FLOAT);
		}else if(type.equals("long")){
			value = sp.getLong(keyword, XmlInfo.DEFAULT_LONG);
		}
		return value;
	}
}
